package com.miao.shop.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.miao.common.entity.R;
import com.miao.common.entity.pojo.Address;
import com.miao.shop.entity.vo.AddressVO;
import com.miao.shop.service.ShopAddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 收货地址表(Address)表控制层
 *
 * @author MiaoWenQing
 * @since 2021-02-14 23:09:49
 */
@RestController
@RequestMapping("common/address")
public class ShopAddressController  {
    /**
     * 服务对象
     */
    @Resource
    private ShopAddressService shopAddressService;


    /**
     * 添加收货地址
     * @param addressVO 添加收货地址
     * @return R
     */
    @PostMapping("add")
    public R add(@RequestBody AddressVO addressVO){
        String[] split = addressVO.getArea().split("-");
        Address address = new Address();
        address.setCusId(Integer.parseInt(addressVO.getCusId()));
        address.setAddrPost(addressVO.getPost());
        address.setAddrTel(addressVO.getTel());
        address.setAddrDetailed(addressVO.getAreaDetail());
        address.setAddrConsignee(addressVO.getName());

        address.setAddrProvince(split[0]);
        address.setAddrCity(split[1]);
        address.setAddrDistrict(split[2]);
        address.setAddrTowns(split[3]);
        if (this.shopAddressService.save(address)) {
            return R.ok();
        }
        return R.error();


    }

    @PostMapping("/edit")
    public R edit(@RequestBody AddressVO addressVO){
        String[] split = addressVO.getArea().split("-");
        Address byId = this.shopAddressService.getById(addressVO.getAddrId());
        Address address = new Address();
        address.setCusId(Integer.parseInt(addressVO.getCusId()));
        address.setAddrPost(addressVO.getPost());
        address.setAddrTel(addressVO.getTel());
        address.setAddrDetailed(addressVO.getAreaDetail());
        address.setAddrConsignee(addressVO.getName());
        address.setAddrType(byId.getAddrType());
        address.setAddrId(addressVO.getAddrId());

        address.setAddrProvince(split[0]);
        address.setAddrCity(split[1]);
        address.setAddrDistrict(split[2]);
        address.setAddrTowns(split[3]);

        if (this.shopAddressService.updateById(address)) {
            return R.ok();
        }
        return R.error();

    }


    /**
     *  设置默认地址
     * @param cusId 消费者id
     * @param addrId 地址id
     * @return R
     */
    @GetMapping("/setDefault")
    public R setDefault(String cusId,String addrId){
        if (this.shopAddressService.setDefault(cusId,addrId)) {
            return R.ok();
        }
        return R.error();

    }


    /**
     * 删除地址
     * @param addrId 地址id
     * @return R
     */
    @GetMapping("del")
    public R del(String addrId){
        if (this.shopAddressService.removeById(addrId)) {
            return R.ok();
        }
        return R.error();
    }


    /**
     * 根据用户id 获取地址
     * @param cusId 用户id
     * @return R
     */
    @GetMapping("getAddress")
    public R getAddress(String cusId){
        QueryWrapper<Address> addressQueryWrapper = new QueryWrapper<>();
        addressQueryWrapper.eq("cus_id",cusId);
        List<Address> list = this.shopAddressService.list(addressQueryWrapper);
        ArrayList<AddressVO> addressVOS = new ArrayList<>();
        list.forEach(item->{
            AddressVO addressVO = new AddressVO();
            addressVO.setTel(item.getAddrTel());
            addressVO.setAreaDetail(item.getAddrDetailed());
            addressVO.setName(item.getAddrConsignee());
            addressVO.setArea(item.getAddrProvince()+"-"+item.getAddrCity()+"-"+item.getAddrDistrict()+"-"+item.getAddrTowns());
            addressVO.setPost(item.getAddrPost());
            addressVO.setAddrId(item.getAddrId());
            addressVO.setCusId(String.valueOf(item.getCusId()));
            addressVO.setType(item.getAddrType());
            addressVOS.add(addressVO);

        });

        return R.ok(addressVOS);

    }



    @GetMapping("addrById")
    public R getOne(String addrId){
        Address address = this.shopAddressService.getById(addrId);
        if (null!=address){
            return R.ok(address);
        }
        return R.error();

    }






}
